# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/14 19:39
# @Author   : Robot-Zsj
"""
description:

i：
3
1 1
1 2
1 3

o:
3
"""

import heapq
import sys

n = int(input())
ranges = [list(map(int, input().split())) for _ in range(n)]


def getResult():
    ranges.sort(key=lambda x: -x[1])

    pq = []

    pq_end = sys.maxsize

    count = 0

    for start, end in ranges:

        while len(pq) > 0 and end < pq_end:

            if -heapq.heappop(pq) <= pq_end:
                count += 1
                pq_end -= 1

        heapq.heappush(pq, -start)
        pq_end = end

    while len(pq) > 0:
        if -heapq.heappop(pq) <= pq_end:
            count += 1
            pq_end -= 1
    return count


print(getResult())
